using System;
using System.Data;
using System.Collections.Generic;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using THSLib;
using THSERP.BaseProvider;
using THSERP.BusinessEntities;

namespace THSERP.Modules.Item
{
    public class ItemEntities : ERPModuleEntities
    {
        #region Declare Constant
        public const String strICProductsObjectName = "ICProducts";

        public const String strICProductAlternativesObjectName = "ICProductAlternatives";
        public const String strICProductComponentsObjectName = "ICProductComponents";
        public const String strICProductAttributesObjectName = "ICProductAttributes";

        public const String strICProductQuantitativesObjectName = "ICProductQuantitatives";
        #endregion
        
        #region Declare all entities variables
        private ICProductsInfo _iCProductsObject;
        private ICProductAlternativesInfo _iCProductAlternativesObject;
        private ICProductComponentsInfo _iCProductComponentsObject;
        private ICProductAttributesInfo _iCProductAttributesObject;
        private ICProductPricesInfo _iCProductPricesObject;
        private ICProductQuantitativesInfo _iCProductQuantitativesObject;
 
        public THSList<ICProductAlternativesInfo> lstICProductAlternatives;
        public THSList<ICProductComponentsInfo> lstICProductComponents;
        public ProductAttributeList lstICProductAttributes;
        public THSList<ICProductPricesInfo> lstICProductPrices;
        public THSItemsEntityList<ProductQuantitativeItemsEntity> lstICProductQuantitatives;
        #endregion

        #region Public Properties
        public ICProductsInfo ICProductsObject
        {
            get
            {
                return _iCProductsObject;
            }
            set
            {
                _iCProductsObject = value;
            }
        }
        public ICProductAlternativesInfo iCProductAlternativesObject
        {
            get
            {
                return _iCProductAlternativesObject;
            }
            set
            {
                _iCProductAlternativesObject = value;
            }
        }
        public ICProductComponentsInfo iCProductComponentsObject
        {
            get
            {
                return _iCProductComponentsObject;
            }
            set
            {
                _iCProductComponentsObject = value;
            }
        }

        public ICProductAttributesInfo ICProductAttributesObject
        {
            get
            {
                return _iCProductAttributesObject;
            }
            set
            {
                _iCProductAttributesObject = value;
            }
        }

        public ICProductPricesInfo ICProductPricesObject
        {
            get 
            {
                return _iCProductPricesObject;
            }
            set
            {
                _iCProductPricesObject = value;
            }
        }

        public ICProductQuantitativesInfo ICProductQuantitativesObject
        {
            get
            {
                return _iCProductQuantitativesObject;
            }
            set { _iCProductQuantitativesObject = value; }
        }

        public THSList<ICProductAlternativesInfo> ICProductAlternativesList
        {
            get
            {
                return lstICProductAlternatives;
            }
            set
            {
                lstICProductAlternatives = value;
            }
        }      
        public THSList<ICProductComponentsInfo> ICProductComponentsList
        {
            get
            {
                return lstICProductComponents;
            }
            set
            {
                lstICProductComponents = value;
            }
        }

        public ProductAttributeList ICProductAttributesList
        {
            get
            {
                return lstICProductAttributes;
            }
            set
            {
                lstICProductAttributes = value;
            }
        }

        public THSList<ICProductPricesInfo> ICProductPricesList
        {
            get
            {
                return lstICProductPrices;
            }
            set
            {
                lstICProductPrices = value;
            }
        }
        public THSItemsEntityList<ProductQuantitativeItemsEntity> ICProductQuantitativesList
        {
            get { return lstICProductQuantitatives; }
            set { lstICProductQuantitatives = value; }
        }
        #endregion

        #region Constructor
        public ItemEntities()
            : base()
        {
            _iCProductsObject = new ICProductsInfo();

            _iCProductAlternativesObject = new ICProductAlternativesInfo();
            _iCProductComponentsObject = new ICProductComponentsInfo();
            _iCProductAttributesObject = new ICProductAttributesInfo();
            _iCProductPricesObject = new ICProductPricesInfo();
            _iCProductQuantitativesObject = new ICProductQuantitativesInfo();

            lstICProductAlternatives = new THSList<ICProductAlternativesInfo>();
            lstICProductComponents = new THSList<ICProductComponentsInfo>();
            lstICProductAttributes = new ProductAttributeList();
            lstICProductPrices = new THSList<ICProductPricesInfo>();
            lstICProductQuantitatives = new THSItemsEntityList<ProductQuantitativeItemsEntity>();
        }

        #endregion

        #region Init Main Object,Module Objects functions
        public override void InitMainObject()
        {

            MainObject = ICProductsObject;
        }

        public override void InitModuleObjects()
        {
            ModuleObjects.Add("ICProductAlternatives", iCProductAlternativesObject);
            ModuleObjects.Add("ICProductComponents", iCProductComponentsObject);
            ModuleObjects.Add("ICProductAttributes", ICProductAttributesObject);
            ModuleObjects.Add("ICProductPrices", ICProductPricesObject);
            ModuleObjects.Add("ICProductQuantitatives", ICProductQuantitativesObject);
        }

        public override void InitModuleObjectList()
        {            
            lstICProductAlternatives = new THSList<ICProductAlternativesInfo>();
            lstICProductAlternatives.InitTHSList(this, "ICProducts", "ICProductAlternatives");

            lstICProductComponents = new THSList<ICProductComponentsInfo>();
            lstICProductComponents.InitTHSList(this, "ICProducts", "ICProductComponents");

            lstICProductAttributes.InitTHSList(this, ItemEntities.strICProductsObjectName, ItemEntities.strICProductAttributesObjectName);

            lstICProductPrices = new THSList<ICProductPricesInfo>();
            lstICProductPrices.InitTHSList(this, "ICProducts", "ICProductPrices");

            lstICProductQuantitatives = new THSItemsEntityList<ProductQuantitativeItemsEntity>();
            lstICProductQuantitatives.InitTHSList(this, "ICProducts", "ICProductQuantitatives");
        }

        public override void InitGridControlInTHSList()
        {
            lstICProductAlternatives.InitTHSListGridControl();
            lstICProductComponents.InitTHSListGridControl();
            lstICProductAttributes.InitTHSTreeListControl();
            lstICProductQuantitatives.InitTHSListGridControl();
        }

        public override void SetDefaultModuleObjectsList()
        {
            try
            {
                lstICProductAlternatives.SetDefaultListAndRefreshGridControl();
                lstICProductComponents.SetDefaultListAndRefreshGridControl();
                lstICProductAttributes.SetDefaultListAndRefreshTreeListControl();
                lstICProductQuantitatives.SetDefaultListAndRefreshGridControl();

                InvalidateProductPriceList();
            }
            catch (Exception)
            {
                return;
            }
        }

        /// <summary>
        /// Synchronize product price levels with default one
        /// </summary>
        /// <remarks>Hieu.nguyen_20/3/2010</remarks>
        /// 
        public void InvalidateProductPriceList()
        {
            
            ARPriceLevelsController objPriceLevelsController = new ARPriceLevelsController();
            String lookupTableName = THSUtil.GetTableNameFromBusinessObjectType(typeof(ARPriceLevelsInfo));
            THSApp.InitDataToLookupTable(lookupTableName);
            DateTime dtLastCreatedDate = THSDbUtil.GetLastCreatedDateOfTable(lookupTableName);
            DateTime dtLastUpdatedDate = THSDbUtil.GetLastUpdatedDateOfTable(lookupTableName);
            if (dtLastCreatedDate.CompareTo(((DateTime)THSApp.LookupTablesUpdatedDate[lookupTableName])) > 0 ||
                dtLastUpdatedDate.CompareTo(((DateTime)THSApp.LookupTablesUpdatedDate[lookupTableName])) > 0)
            {
                DataSet ds = objPriceLevelsController.GetAllObjects();
                if (ds.Tables.Count > 0)
                {
                    THSApp.LookupTablesUpdatedDate[lookupTableName] = DateTime.Now;
                    ((DataSet)THSApp.LookupTables[lookupTableName]).Tables.Clear();
                    ((DataSet)THSApp.LookupTables[lookupTableName]).Tables.Add(ds.Tables[0].Copy());
                }
            }

            THSList<ARPriceLevelsInfo> lstPriceLevels = new THSList<ARPriceLevelsInfo>();
            lstPriceLevels.InitTHSList(this, String.Empty, lookupTableName);
            DataTable lookupTable = ((DataSet)THSApp.LookupTables[lookupTableName]).Tables[0];
            foreach (DataRow row in lookupTable.Rows)
            {
                ARPriceLevelsInfo objPriceLevelsInfo = (ARPriceLevelsInfo)objPriceLevelsController.GetObjectFromDataRow(row);
                lstPriceLevels.Add(objPriceLevelsInfo);
            }

            //Add new price levels to product price level list
            ICProductsInfo objProductsInfo = (ICProductsInfo)MainObject;
            foreach (ARPriceLevelsInfo objPriceLevelsInfo in lstPriceLevels)
                if (!ICProductPricesList.Exists("FK_ARPriceLevelID", objPriceLevelsInfo.ARPriceLevelID))
                {
                    ICProductPricesInfo objProductPricesInfo = new ICProductPricesInfo();
                    objProductPricesInfo.FK_ARPriceLevelID = objPriceLevelsInfo.ARPriceLevelID;
                    objProductPricesInfo.FK_ICProductID = objProductsInfo.ICProductID;
                    objProductPricesInfo.ICProductPriceMarkDown = objPriceLevelsInfo.ARPriceLevelMarkDown;
                    ICProductPricesList.Add(objProductPricesInfo);
                }

            //Delete old price levels from product price level list
            for (int i = 0; i < ICProductPricesList.Count; i++)
                if (!lstPriceLevels.Exists("ARPriceLevelID", ICProductPricesList[i].FK_ARPriceLevelID))
                {
                    ICProductPricesList.RemoveAt(i);
                    i--;
                }
        }
        public override void DuplicateModuleObjectList()
        {
            lstICProductAttributes.Duplicate();
            lstICProductAttributes.TreeListControl.RefreshDataSource();
            lstICProductPrices.Duplicate();
            lstICProductQuantitatives.Duplicate();
        }
        #endregion

        #region Invalidate Module Objects functions
        public override void InvalidateModuleObject(BusinessObject obj)
        {
            base.InvalidateModuleObject(obj);
            if (obj.GetType() == typeof(ICProductAttributesInfo))
            {
                ICProductAttributesInfo objProductAttributesInfo = (ICProductAttributesInfo)obj;
                objProductAttributesInfo.ICInventoryStockAvailableQuantity = objProductAttributesInfo.ICInventoryStockQuantity - objProductAttributesInfo.ICInventoryStockSaleOrderQuantity;
                Module.DisplayLabelText(objProductAttributesInfo);
            }
        }

        public override void InvalidateModuleObjects(int iObjectID)
        {
            ICProductAlternativesList.Invalidate(iObjectID);
            ICProductComponentsList.Invalidate(iObjectID);
            ICProductAttributesList.InvalidateTreeList(iObjectID, true);
            ICProductPricesList.Invalidate(iObjectID);
            InvalidateProductPriceList();
            ICProductQuantitativesList.Invalidate(iObjectID);
        }
        #endregion

        #region Save Module Objects functions
        public override void SaveModuleObjects()
        {
            ICProductAlternativesList.SaveItemObjects(true);
            ICProductComponentsList.SaveItemObjects(true);

            ICProductPricesList.SaveItemObjects();

            ICProductQuantitativesList.SaveItemObjects();
        }
        #endregion

        public override void DeleteObjectRelations(string strTableName, int iObjectID)
        {
            ICProductAttributesController objProductAttributesController = new ICProductAttributesController();
            switch (strTableName)
            {
                case ItemEntities.strICProductsObjectName:
                    objProductAttributesController.DeleteByForeignColumn("FK_ICProductID", iObjectID);
                    break;
                case ItemEntities.strICProductAttributesObjectName:
                    DataSet ds = objProductAttributesController.GetAllObjectsByObjectParentID(iObjectID);
                    if (ds.Tables.Count > 0)
                    {
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            DeleteObjectRelations(strTableName, Convert.ToInt32(row["ICProductAttributeID"]));
                        }
                    }
                    objProductAttributesController.DeleteAllObjectsByObjectParentID(iObjectID);
                    break;
            }
        }
        public override void SetValuesAfterValidateProduct(int iICProductID)
        {
            //ICProductQuantitativesList.SetDefaultValuesFromProduct(iICProductID);

            ICProductsInfo objICProductsInfo = (ICProductsInfo)MainObject;
            ICProductQuantitativesInfo objICProductQuantitativesInfo = (ICProductQuantitativesInfo)ModuleObjects[strICProductQuantitativesObjectName];
            ICProductsInfo objICProduct = (ICProductsInfo)new ICProductsController().GetObjectByID(iICProductID);
            objICProductQuantitativesInfo.ICProductQuantitativeQty = 1;
            objICProductQuantitativesInfo.ICProductQuantitativeUOM = objICProduct.ICProductBasicUnit;
            objICProductQuantitativesInfo.ICProductQuantitativePrice = objICProduct.ICProductSupplierPrice;
            
            UpdateModuleObjectBindingSource(strICProductQuantitativesObjectName);
        }
        public override void SetValuesAfterValidateProductQty()
        {
            ICProductsInfo objICProductsInfo = (ICProductsInfo)MainObject;
            ICProductQuantitativesInfo objICProductQuantitativesInfo = (ICProductQuantitativesInfo)ModuleObjects[strICProductQuantitativesObjectName];
            ICProductsInfo objICProduct = (ICProductsInfo)new ICProductsController().GetObjectByID(objICProductQuantitativesInfo.FK_ICProductMaterialID);
            objICProductQuantitativesInfo.ICProductQuantitativePrice = objICProductQuantitativesInfo.ICProductQuantitativeQty * objICProduct.ICProductSupplierPrice;
            UpdateModuleObjectBindingSource(strICProductQuantitativesObjectName);
        }
    }
}
